package com.qtjf.qtmoneyservice.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qtjf.qtmoneyservice.MyApplication;
import com.qtjf.qtmoneyservice.R;
import com.qtjf.qtmoneyservice.constantSet.Constant;
import com.qtjf.qtmoneyservice.domain.RelateBean;
import com.qtjf.qtmoneyservice.widget.AddressDialog;
import com.qtjf.qtmoneyservice.widget.RelateDialog;
import com.qtjf.qtmoneyservice.widget.SheHuiDialog;

import java.util.HashMap;
import java.util.Map;

public class RelateActivity extends BaseActivity {

    ImageView mBack;

    TextView relateText;
    TextView shehuiText;
    EditText phoneText1;
    EditText phoneText2;
    TextView submitBt;
    String authenticationId;
    RelateBean relateBean;
    Gson gson;


    Handler handler=new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            if(msg.arg1==9)
            {
                relateText.setText(msg.obj.toString());
            }
            else if(msg.arg1==10)
            {
                shehuiText.setText(msg.obj.toString());
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relate);

        super.setTranslucent();
        Intent intent=getIntent();
        authenticationId=intent.getStringExtra("authenticationId");

        initView();
        initData();
        initEvent();
    }


    private void initView() {
        gson=new Gson();
        mBack = (ImageView) findViewById(R.id.back_bt);
        relateText= (TextView) findViewById(R.id.relate_text);
        shehuiText= (TextView) findViewById(R.id.shehui_text);
        phoneText1= (EditText) findViewById(R.id.phone_text1);
        phoneText2= (EditText) findViewById(R.id.phone_text2);
        submitBt= (TextView) findViewById(R.id.submit_bt);
    }

    private void initData() {
        getInfo();
    }

    private void initEvent() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RelateActivity.this.finish();
            }
        });

        relateText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                RelateDialog dialog=new RelateDialog(RelateActivity.this,R.style.myTransparent,handler);
                dialog.show();

            }
        });


        shehuiText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                SheHuiDialog dialog=new SheHuiDialog(RelateActivity.this,R.style.myTransparent,handler);
                dialog.show();
            }
        });

        submitBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submitInfo();
            }
        });
    }


    private void submitInfo() {
        String url= Constant.HOMEADDRESS + "jdt/submitEmergencyContract";
        final StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
                Toast.makeText(RelateActivity.this, response, Toast.LENGTH_SHORT).show();
                if(response.length()>10) {

                    //  Toast.makeText(LoginActivity.this, token.getToken(), Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Log.e("error",error.toString());
                Toast.makeText(RelateActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        }) {
            // 需要重写获取参数的函数,可以向服务器提交参数
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("userId","1");
                map.put("relationship",relateText.getText().toString());
                map.put("relationshipPhone",phoneText1.getText().toString());
                map.put("social",shehuiText.getText().toString());
                map.put("socialPhone",phoneText2.getText().toString());
                return map;
            }

        };


        MyApplication.getQueue().add(stringRequest);

    }

    //GET 开发完成 http://118.89.185.176:8082/jdt/getAuthenticationInfo?type={type}&id={id}
    private void getInfo() {
        String url= Constant.HOMEADDRESS + "jdt/getAuthenticationInfo?type="+"2"+"&id="+authenticationId;
        final StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
                Toast.makeText(RelateActivity.this, response, Toast.LENGTH_SHORT).show();
               /* Log.d("response",response);
                Log.d("response",authenticationId);*/
                if(response.length()>10) {
                    relateBean = gson.fromJson(response, new TypeToken<RelateBean>() {
                    }.getType());

                    if(relateBean.getData().getRelationship()!=null)
                    {
                        relateText.setText(relateBean.getData().getRelationship());
                    }

                    if(relateBean.getData().getSocial()!=null)
                    {
                        shehuiText.setText(relateBean.getData().getSocial());
                    }

                    if(relateBean.getData().getRelationshipPhone()!=null)
                    {
                        phoneText1.setText((String)relateBean.getData().getRelationshipPhone());
                    }

                    if(relateBean.getData().getSocialPhone()!=null)
                    {
                        phoneText2.setText((String)relateBean.getData().getSocialPhone());
                    }
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(RelateActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        }) ;


        MyApplication.getQueue().add(stringRequest);

    }
}
